// pages/withdrawal/withdrawal.js
import imgUrl from '../wallet/imgUtil.js'
import { clearNoNum }  from "./moneyFilter.js"
import { Wallet } from "../wallet/wallet.js"
let app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      rightIcon:imgUrl.rightIcon,
      account:"",
      balance:0,
      isValue:false,
      isSubmit:false,
      currentBank:{},
      bankName:"亲,还未设置提现账户"
  },
  clickInput(){
    this.setData({
      isValue:true
    })
  },
  onblur(){
    // this.setData({
    //   isValue:false
    // })
  },
  jumpBindCard(){
    Wallet._getBankCard(res=>{
      // console.log("111",res)
    
       let length = res.filter(item=>{
        return item.isDefault == 1
       }).length
       if(length == 0){
          wx.navigateTo({
            url: '../setBankAccount/setBankAccount?id=2',
          })

       }else{
          wx.navigateTo({
            url: '../bindBankCard/bindBankCard',
          })
       }
  })

  
  },
  toSubmit(){
    if(this.data.isSubmit){
       if(this.data.currentBank.bankCardId){
        let data={
          bankCardId:this.data.currentBank.bankCardId,
          walletPwd:"",
          withdrawMoney:this.data.account
        }

        Wallet._toWithdrawal(data,res=>{
          this.setData({
            isValue:false,
            account:""
          })

          wx.showToast({
            title: '提现申请已提交！',
          })
          setTimeout(()=>{
            wx.navigateTo({
              url:"../withdrawalSuccess/withdrawalSuccess"
            })
          },1500)

         
        })
       }else{
         wx.showToast({
           title: '请设置默认提现账户',
         })
       }
       

       
    } 
  },
  jumpWithdrawalRecord(){
    wx.navigateTo({
      url: '../withdrawalRecord/withdrawalRecord',
    })
  },
  listenValue(e){
    //  console.log(clearNoNum(e.detail.value))
    
    
     if(e.detail.value == ""){
       this.setData({
          isValue:false,
          account:""
       })
     }else{
      this.setData({
        isValue:true,
        account:clearNoNum(e.detail.value)
     })
     }

     if(this.data.account == 0 ){
      this.setData({
        isSubmit:false,   
      })
     }else
     if(this.data.account == 0. ){
      this.setData({
        isSubmit:false,   
      })
     }else
     if(this.data.account == 0.0 ){
      this.setData({
        isSubmit:false,   
      })
     }else
     if(this.data.account == 0.00 ){
      this.setData({
        isSubmit:false,   
      })
     }else{
      this.setData({
        isSubmit:true,   
      })
     }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(){
       new Wallet(res=>{
          console.log(res.data)
          this.setData({
            balance:res.data.balance
          })


          Wallet._getBankCard(res=>{
              console.log("111",res)
            
               let length = res.filter(item=>{
                return item.isDefault == 1
               }).length
               if(!app.globalData.tempBankCard.bankCardId ){
                if(length == 0){
                  // wx.navigateTo({
                  //   url: '../setBankAccount/setBankAccount',
                  // })

             }else{
                
                 this.setData({
                    currentBank:(res.filter(item=>{
                      return item.isDefault == 1
                     })[0])||{},
                     bankName:res.filter(item=>{
                      return item.isDefault == 1
                     })[0].bankName||""
                  })
                  console.log(this.data.currentBank)

             }
              }else{
                this.setData({
                  currentBank:app.globalData.tempBankCard,
                   bankName:app.globalData.tempBankCard.bankName
                })
              }


              
            
               
                
              



          })

       })
  },
  withdrawalAll(){
       this.setData({
         isValue:true,
         account:this.data.balance
       })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})